<?php

namespace backend\modules\demo\models;

use yii\elasticsearch\ActiveRecord;

class Customer extends ActiveRecord
{
    // Other class attributes and methods go here
    // ...

    /**
     * @return array This model's mapping
     */
    public static function mapping(): array
    {
        return [
            // Field types: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html#field-datatypes
            'properties' => [
                'first_name'     => ['type' => 'text'],
                'last_name'      => ['type' => 'text'],
                'order_ids'      => ['type' => 'keyword'],
                'email'          => ['type' =>'keyword'],
                'content'        => ['type' =>'keyword'],
                'registered_at'  => ['type' => 'date'],
                'updated_at'     => ['type' => 'date'],
                'status'         => ['type' => 'keyword'],
                'age'            => ['type' => 'long'],
                'is_active'      => ['type' => 'boolean'],
                'title'          => ['type' => 'completion'],//suggest建议用
                'ip_addr'        => ['type' => 'ip'],
                'birthed'        => ['type' => 'integer_range'],//32-bit integers
            ]
        ];
    }

    /**
     * Set (update) mappings for this model
     */
    public static function updateMapping()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->setMapping(static::index(), static::type(), static::mapping());
    }

    /**
     * Create this model's index
     */
    public static function createIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->createIndex(static::index(), [
            //'aliases' => [ /* ... */ ],
            'mappings' => static::mapping(),
            //'settings' => [ /* ... */ ],
        ]);
    }

    /**
     * Delete this model's index
     */
    public static function deleteIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->deleteIndex(static::index(), static::type());
    }

    public static function addAlias(){
        $db = static::getDb();
        $command = $db->createCommand();
        $command->addAlias(static::index(),static::index().'_1');
    }

    //切换elastic 索引
    public static function changeAlias(){
        $db = static::getDb();
        $command = $db->createCommand();
        $alias = key($command->getIndexInfoByAlias('customers'));

        if($alias == 'customer-alias2s' || empty($alias)){
            $actions = [
                ['remove' => ['index' => 'customer-alias2s', 'alias' => 'customers']],
                ['add' =>    ['index' => 'customer-alias1s', 'alias' => 'customers']],
            ];
        }else{
            $actions = [
                ['remove' => ['index' => 'customer-alias1s', 'alias' => 'customers']],
                ['add' =>    ['index' => 'customer-alias2s', 'alias' => 'customers']],
            ];
        }

        $db = static::getDb();
        $command = $db->createCommand();
        $command->aliasActions($actions);
    }

    //用别名切换索引
    public static function aliasActions(){
         $actions = [
             ['remove' => ['index' => 'customer-alias2s', 'alias' => 'customers_active']],
             ['add' =>    ['index' => 'customer-alias1s', 'alias' => 'customers_active']],
        ];
        $db = static::getDb();
        $command = $db->createCommand();
        $command->aliasActions($actions);
    }

    public static function getAliasInfo(){
        $db = static::getDb();
        $command = $db->createCommand();
        $alias = $command->getIndexAliases('customers');
        print_r($alias);die;
    }
    //批量操作数据
    public static function bulkData(){
        $db = static ::getDb();
        $command = $db->createBulkCommand();
        $command->index = 'customers';


        $command->addAction(["index" => ["_id" => 1]], ['name'=>'sunbo']);
        $command->addAction(["index" => ["_id" => 2]], ['name'=>'testsunbo']);
        $command->execute();


    }

}